package cn.iocoder.yudao.module.pplan.service.producerecord;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pplan.controller.admin.producerecord.vo.ProduceRecordPageReqVO;
import cn.iocoder.yudao.module.pplan.controller.admin.producerecord.vo.ProduceRecordRespVO;
import cn.iocoder.yudao.module.pplan.controller.admin.producerecord.vo.ProduceRecordSaveReqVO;
import cn.iocoder.yudao.module.pplan.controller.admin.producerecord.vo.ProduceRecordUpdateStatusReqVO;
import cn.iocoder.yudao.module.pplan.controller.app.producerecord.vo.AppProduceRecordPageReqVO;
import cn.iocoder.yudao.module.pplan.controller.app.producerecord.vo.AppProduceRecordRespVO;
import cn.iocoder.yudao.module.pplan.controller.app.producerecord.vo.AppProduceRecordSaveReqVO;
import cn.iocoder.yudao.module.pplan.dal.dataobject.producerecord.ProduceRecordDO;
import cn.iocoder.yudao.module.pplan.dal.mysql.producerecord.ProduceRecordMapper;
import cn.iocoder.yudao.module.pplan.service.recipe.RecipeService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pplan.enums.ErrorCodeConstants.PRODUCE_RECORD_NOT_EXISTS;

/**
 * 生产计划-生产计划记录 Service 实现类
 *
 * @author yisl
 */
@Service
@Validated
public class ProduceRecordServiceImpl implements ProduceRecordService {

    @Resource
    private ProduceRecordMapper produceRecordMapper;
    @Resource
    private RecipeService recipeService;

    @Override
    public Long createProduceRecord(ProduceRecordSaveReqVO createReqVO) {
        // 插入
        ProduceRecordDO produceRecord = BeanUtils.toBean(createReqVO, ProduceRecordDO.class);
        produceRecordMapper.insert(produceRecord);
        // 返回
        return produceRecord.getId();
    }

    @Override
    public void updateProduceRecord(ProduceRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateProduceRecordExists(updateReqVO.getId());
        // 更新
        ProduceRecordDO updateObj = BeanUtils.toBean(updateReqVO, ProduceRecordDO.class);
        produceRecordMapper.updateById(updateObj);
    }

    @Override
    public void updateProduceStatus(ProduceRecordUpdateStatusReqVO updateReqVO) {
        // 校验存在
        validateProduceRecordExists(updateReqVO.getId());
        // 更新
        ProduceRecordDO updateObj = BeanUtils.toBean(updateReqVO, ProduceRecordDO.class);
        produceRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteProduceRecord(Long id) {
        // 校验存在
        validateProduceRecordExists(id);
        // 删除
        produceRecordMapper.deleteById(id);
    }

    @Override
    public void deleteProduceRecordListByIds(List<Long> ids) {
        // 校验存在
        validateProduceRecordExists(ids);
        // 删除
        produceRecordMapper.deleteByIds(ids);
    }

    private void validateProduceRecordExists(List<Long> ids) {
        List<ProduceRecordDO> list = produceRecordMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(PRODUCE_RECORD_NOT_EXISTS);
        }
    }

    private void validateProduceRecordExists(Long id) {
        if (produceRecordMapper.selectById(id) == null) {
            throw exception(PRODUCE_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public ProduceRecordRespVO getProduceRecordByFn(String flightNo) {
        ProduceRecordDO produceRecordDO = produceRecordMapper.selectFirstOne(ProduceRecordDO::getFlightNo, flightNo);
        if (produceRecordDO == null) {
            throw exception(PRODUCE_RECORD_NOT_EXISTS);
        }
        return getProduceRecordUtils(produceRecordDO);
    }

    @Override
    public ProduceRecordRespVO getProduceRecord(Long id) {
        ProduceRecordDO produceRecordDO = produceRecordMapper.selectById(id);
        if (produceRecordDO == null) {
            throw exception(PRODUCE_RECORD_NOT_EXISTS);
        }
        return getProduceRecordUtils(produceRecordDO);
    }

    public ProduceRecordRespVO getProduceRecordUtils(ProduceRecordDO produceRecordDO){
        ProduceRecordRespVO produceRecordRespVO = BeanUtils.toBean(produceRecordDO, ProduceRecordRespVO.class);
        if (ObjectUtil.isNotEmpty(produceRecordRespVO.getBussClassMeal())){
            produceRecordRespVO.setBussClassRecipe(recipeService.appGetRecipe(Long.valueOf(produceRecordRespVO.getBussClassMeal())));
        }
        if (ObjectUtil.isNotEmpty(produceRecordRespVO.getPremEcoClassMeal())){
            produceRecordRespVO.setPremEcoClassRecipe(recipeService.appGetRecipe(Long.valueOf(produceRecordRespVO.getPremEcoClassMeal())));
        }
        if (ObjectUtil.isNotEmpty(produceRecordRespVO.getEcoClassMeal())){
            produceRecordRespVO.setEcoClassRecipe(recipeService.appGetRecipe(Long.valueOf(produceRecordRespVO.getEcoClassMeal())));
        }
        if (ObjectUtil.isNotEmpty(produceRecordRespVO.getBussClassSpcMeal())){
            produceRecordRespVO.setBussClassSpcRecipe(recipeService.appGetRecipe(Long.valueOf(produceRecordRespVO.getBussClassSpcMeal())));
        }
        if (ObjectUtil.isNotEmpty(produceRecordRespVO.getPremEcoClassSpcMeal())){
            produceRecordRespVO.setPremEcoClassSpcRecipe(recipeService.appGetRecipe(Long.valueOf(produceRecordRespVO.getPremEcoClassSpcMeal())));
        }
        if (ObjectUtil.isNotEmpty(produceRecordRespVO.getEcoClassSpcMeal())){
            produceRecordRespVO.setEcoClassSpcRecipe(recipeService.appGetRecipe(Long.valueOf(produceRecordRespVO.getEcoClassSpcMeal())));
        }
        if (ObjectUtil.isNotEmpty(produceRecordRespVO.getCrewMeal())){
            produceRecordRespVO.setCrewRecipe(recipeService.appGetRecipe(Long.valueOf(produceRecordRespVO.getCrewMeal())));
        }
        if (ObjectUtil.isNotEmpty(produceRecordRespVO.getCrewSpcMeal())){
            produceRecordRespVO.setCrewSpcRecipe(recipeService.appGetRecipe(Long.valueOf(produceRecordRespVO.getCrewSpcMeal())));
        }

        return produceRecordRespVO;
    }

    @Override
    public PageResult<ProduceRecordDO> getProduceRecordPage(ProduceRecordPageReqVO pageReqVO) {
        return produceRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public void appUpdateProduceRecord(AppProduceRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateProduceRecordExists(updateReqVO.getId());
        // 更新
        ProduceRecordDO updateObj = BeanUtils.toBean(updateReqVO, ProduceRecordDO.class);
        produceRecordMapper.updateById(updateObj);
    }

    @Override
    public AppProduceRecordRespVO appGetProduceRecord(Long id) {
        ProduceRecordDO produceRecordDO = produceRecordMapper.selectById(id);
        if (produceRecordDO == null) {
            throw exception(PRODUCE_RECORD_NOT_EXISTS);
        }
        AppProduceRecordRespVO appProduceRecordRespVO = BeanUtils.toBean(produceRecordDO, AppProduceRecordRespVO.class);
        if (ObjectUtil.isNotEmpty(appProduceRecordRespVO.getBussClassMeal())){
            appProduceRecordRespVO.setBussClassSpcRecipe(recipeService.appGetRecipe(Long.valueOf(appProduceRecordRespVO.getBussClassMeal())));
        }
        if (ObjectUtil.isNotEmpty(appProduceRecordRespVO.getPremEcoClassMeal())){
            appProduceRecordRespVO.setEcoClassRecipe(recipeService.appGetRecipe(Long.valueOf(appProduceRecordRespVO.getPremEcoClassMeal())));
        }
        if (ObjectUtil.isNotEmpty(appProduceRecordRespVO.getEcoClassMeal())){
            appProduceRecordRespVO.setEcoClassRecipe(recipeService.appGetRecipe(Long.valueOf(appProduceRecordRespVO.getEcoClassMeal())));
        }
        if (ObjectUtil.isNotEmpty(appProduceRecordRespVO.getCrewMeal())){
            appProduceRecordRespVO.setCrewRecipe(recipeService.appGetRecipe(Long.valueOf(appProduceRecordRespVO.getCrewMeal())));
        }

        return appProduceRecordRespVO;
    }

    @Override
    public PageResult<AppProduceRecordRespVO> appGetProduceRecordPage(AppProduceRecordPageReqVO pageReqVO) {
        PageResult<ProduceRecordDO> recordDOPageResult = produceRecordMapper.appSelectPage(pageReqVO);
        return new PageResult<>(BeanUtils.toBean(recordDOPageResult.getList(), AppProduceRecordRespVO.class), recordDOPageResult.getTotal());

    }

}